package de.klingbeil.hhc.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.event.shared.SimpleEventBus;
import com.google.gwt.inject.client.AbstractGinModule;
import com.google.gwt.inject.client.GinModules;
import com.google.gwt.inject.client.Ginjector;
import com.google.inject.Provides;
import com.google.inject.Singleton;

import de.klingbeil.hhc.client.ItemRequestFactory.ItemRequestContext;
import de.klingbeil.hhc.client.WebappGinjector.WebappGinModule;
import de.klingbeil.hhc.client.controller.ApplicationActionBarController;
import de.klingbeil.hhc.client.controller.ApplicationActionBarController.ApplicationActionBarView;
import de.klingbeil.hhc.client.controller.ApplicationEditorController;
import de.klingbeil.hhc.client.controller.ApplicationEditorController.ApplicationEditorView;
import de.klingbeil.hhc.client.controller.ApplicationHeaderController;
import de.klingbeil.hhc.client.controller.ApplicationHeaderController.ApplicationHeaderView;
import de.klingbeil.hhc.client.controller.ApplicationInfoController;
import de.klingbeil.hhc.client.controller.ApplicationInfoController.ApplicationInfoView;
import de.klingbeil.hhc.client.controller.ApplicationListController;
import de.klingbeil.hhc.client.controller.ApplicationListController.ApplicationListView;
import de.klingbeil.hhc.client.controller.ItemEditorController;
import de.klingbeil.hhc.client.controller.ItemEditorController.ItemEditorView;
import de.klingbeil.hhc.client.controller.ItemListController;
import de.klingbeil.hhc.client.controller.ItemListController.ItemListView;
import de.klingbeil.hhc.client.handler.ItemDataHandler;
import de.klingbeil.hhc.client.view.ApplicationActionBarViewImpl;
import de.klingbeil.hhc.client.view.ApplicationEditorViewImpl;
import de.klingbeil.hhc.client.view.ApplicationHeaderViewImpl;
import de.klingbeil.hhc.client.view.ApplicationInfoViewImpl;
import de.klingbeil.hhc.client.view.ApplicationListViewImpl;
import de.klingbeil.hhc.client.view.ItemEditorViewImpl;
import de.klingbeil.hhc.client.view.ItemListViewImpl;
import de.klingbeil.hhc.client.widget.RootContainer;

@GinModules({ WebappGinModule.class })
public interface WebappGinjector extends Ginjector {
	public static class WebappGinModule extends AbstractGinModule {
		@Override
		protected void configure() {
			bind(ApplicationMainPage.class);
			bind(ItemDataHandler.class);
			bind(RootContainer.class);

			bind(ApplicationHeaderController.class);
			bind(ApplicationActionBarController.class);
			bind(ApplicationEditorController.class);
			bind(ApplicationListController.class);
			bind(ApplicationInfoController.class);
			bind(ItemEditorController.class);
			bind(ItemListController.class);

			bind(ApplicationHeaderView.class).to(
					ApplicationHeaderViewImpl.class);
			bind(ApplicationActionBarView.class).to(
					ApplicationActionBarViewImpl.class);
			bind(ApplicationEditorView.class).to(
					ApplicationEditorViewImpl.class);
			bind(ApplicationListView.class).to(ApplicationListViewImpl.class);
			bind(ApplicationInfoView.class).to(ApplicationInfoViewImpl.class);
			bind(ItemEditorView.class).to(ItemEditorViewImpl.class);
			bind(ItemListView.class).to(ItemListViewImpl.class);
		}

		@Provides
		@Singleton
		public ItemRequestFactory getItemRequestFactory(EventBus eventBus) {
			ItemRequestFactory factory = GWT.create(ItemRequestFactory.class);
			factory.initialize(eventBus);
			return factory;
		}

		@Provides
		public ItemRequestContext getItemContext(ItemRequestFactory factory) {
			return factory.context();
		}

		@Provides
		@Singleton
		public EventBus getEventBus() {
			return new SimpleEventBus();
		}
	}

	ApplicationMainPage getMainPage();
}
